package com.naiterui.ehp.bs.patient.vo.consult;

import com.naiterui.ehp.bs.patient.prodocl.Chater;
import com.naiterui.ehp.bs.patient.prodocl.IChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class Message<T> implements IChecker,Serializable {

    private static final long serialVersionUID = -2116776414406853005L;

    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(com.naiterui.ehp.bs.patient.vo.consult.Message.class);

    /**
     * 消息类型
     * 0：文本消息
     * 1：图片
     * 2：音频
     */
    private Integer type;

    /**
     * 发送者Id
     */
    private Chater from;

    /**
     * 接收者Id
     */
    private Chater to;

    /**
     * 0：患者2医生
     * 1：医生2患者
     */
    private Integer relation;

    /**
     * 消息内容
     * 1、一段文字
     * 2、一个URL（图片、音频）
     * 3、药方
     */
    private T content;

    /**
     * 发送时间
     * 单位：毫秒
     */
    private Long sendTime;

    private Session session;

    public class Session {
        private String sessionId;

        Session(String sessionId) {
            this.sessionId = sessionId;
        }

        public String getSessionId() {
            return this.sessionId;
        }

        public void setSessionId(String sessionId) {
            this.sessionId = sessionId;
        }
    }
    /**
     * 检测消息属性的完整性和正确性
     * Message.selfValidate()<BR>
     * <P>Author : zhouyanxin </P>
     * <P>Date : 2015年6月22日 </P>
     * @return
     */
    @Override
    public boolean selfValidate() {

        // 消息基本属性检测
        boolean available = this.from.selfValidate() && this.to.selfValidate() && (this.sendTime != null && this.sendTime > 0)
                && this.content != null;
        if (!available) {
            LOGGER.error("message attribute [sendTime] or [content] is invalid. {}, {}", this.sendTime, this.content);
        }

        // 消息类型如果不是文本消息，将根据各自定义的检测方法进行完整性和正确性的检测
        if (this.content instanceof String) {
            return available;
        } else {
            return available && ((IChecker) this.content).selfValidate();
        }
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Chater getFrom() {
        return from;
    }

    public void setFrom(Chater from) {
        this.from = from;
    }

    public Chater getTo() {
        return to;
    }

    public void setTo(Chater to) {
        this.to = to;
    }

    public Integer getRelation() {
        return relation;
    }

    public void setRelation(Integer relation) {
        this.relation = relation;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public Long getSendTime() {
        return sendTime;
    }

    public void setSendTime(Long sendTime) {
        this.sendTime = sendTime;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }
}